//
//  ByViewMyOrderSectionHeader.m
//  mijifen_ios
//
//  Created by 朱凯 on 2017/11/25.
//  Copyright © 2017年 itboye. All rights reserved.
//

#import "ByViewMyOrderSectionHeader.h"

@implementation ByViewMyOrderSectionHeader
{
    UIImageView *_iconView;             // 主图
    UIView      *_sepLine;              // 两个cell之间的分割
    UILabel     *_titleLabel;           // 标题
    UIView      *_tagsView;             // 标签视图
    UIImageView *_arrowView;            // 箭头
    
    UILabel     *_statusLabel;          // 状态
    
}
- (instancetype)init {
    
    if (self = [super init]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self setupUI];
        // 模拟
        [self createTags:@[@"积分自扣", @"自营"]];
    }
    return self;
}
- (void)setupUI {
    
    // icon
    _iconView = [[UIImageView alloc] init];
    _iconView.backgroundColor = BY_BLUE_COLOR;
    _iconView.layer.cornerRadius = 15;
    _iconView.layer.masksToBounds = true;
    [self addSubview:_iconView];
    
    // 分割
    _sepLine = [[UIView alloc] init];
    _sepLine.backgroundColor = BY_LINE_COLOR;
    [self addSubview:_sepLine];
    
    // 标题
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.numberOfLines = 2;
    _titleLabel.font = FONT(14);
    _titleLabel.text = @"耐克旗舰店";
    _titleLabel.textColor = [UIColor blackColor];
    _titleLabel.textAlignment = NSTextAlignmentLeft;
    [self addSubview:_titleLabel];
    
    // 标签
    _tagsView = [UIView new];
    [self addSubview:_tagsView];
    
    // 箭头
    _arrowView = [[UIImageView alloc] init];
    _arrowView.image = [UIImage imageNamed:@"右箭头"];
    [self addSubview:_arrowView];
    
    // 状态
    _statusLabel = [[UILabel alloc] init];
    _statusLabel.font = FONT(14);
    _statusLabel.text = @"待自取";
    _statusLabel.textColor = BY_RED_COLOR;
    _statusLabel.textAlignment = NSTextAlignmentRight;
    [self addSubview:_statusLabel];
    
    // 布局
    [_iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.size.mas_equalTo(CGSizeMake(30, 30));
        make.centerY.equalTo(@0);
    }];
    [_sepLine mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.equalTo(@0);
        make.height.equalTo(@1);
    }];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(_iconView.mas_right).offset(10);
        make.top.bottom.equalTo(_iconView);
    }];
    [_tagsView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.equalTo(@0);
        make.left.equalTo(_titleLabel.mas_right);
        // tagsView的width由子视图决定
    }];
    [_arrowView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.centerY.equalTo(@0);
        make.left.equalTo(_tagsView.mas_right).offset(0);
    }];
    [_statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(@-10);
        make.centerY.equalTo(@0);
    }];
}


- (void)createTags:(NSArray *)tags {
    
    [_tagsView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    UILabel *last = nil;
    for (int i = 0; i < tags.count; i ++) {
        
        UILabel *label = [[UILabel alloc] init];
        label.text = tags[i];
        label.font = FONT(12);
        label.textAlignment = NSTextAlignmentCenter;
        label.textColor = BY_RED_COLOR;
        label.layer.cornerRadius = 4;
        label.layer.masksToBounds = true;
        label.layer.borderColor = BY_RED_COLOR.CGColor;
        label.layer.borderWidth = 1;
        [_tagsView addSubview:label];
        
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.height.equalTo(@20);
            make.centerY.equalTo(@0);
            make.width.equalTo(@([tags[i] getSizeOfString:12].width + 10));
            if (last) {
                make.left.equalTo(last.mas_right).offset(5);
            } else {
                make.left.equalTo(@5);
            }
            
            if (i == tags.count - 1) {
                // 设置tagsView的宽度
                make.right.equalTo(_tagsView).offset(-5);
            }
        }];
        
        last = label;
    }
}




@end
